package com.coocaa.launcher.framework.manager.homekey;

import android.content.Context;

import com.coocaa.x.app.framework.receiver.HomeKeyReceiver;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.app.CoocaaApplication.CoocaaVersion;
import com.coocaa.x.framework.data.JObject;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.litecontent.LiteContentServiceConnecter;

import java.util.List;

public class HomeKeymanager {
    private static HomeKeymanager mInstance;
    private HomeKeyReceiver mHomeKeyReceiver;
    private Context mContext;

    public static HomeKeymanager getInstance() {
        if (mInstance == null) {
            mInstance = new HomeKeymanager();
        }
        return mInstance;
    }

    /**
     * 注册广播
     */
    public void init(Context comtext) {
        mContext = comtext;
        // 注册广播
        mHomeKeyReceiver = HomeKeyReceiver.create(mContext);
//        mHomeKeyReceiver = new HomeKeyReceiver();
    }

    /**
     * 取消注册
     */
    public void unInit() {
        HomeKeyReceiver.destroy(mContext, mHomeKeyReceiver);
    }

    public void registerHomeKeyListener(HomeKeyReceiver.IHomeKeyBroadcastListener i) {
        mHomeKeyReceiver.registerHomeKeyListener(i);
    }

    public void unRegisterHomeKeyListener(HomeKeyReceiver.IHomeKeyBroadcastListener i) {
        mHomeKeyReceiver.unRegisterHomeKeyListener(i);
    }

    public boolean isCooCaaVer5() {
        boolean isCooCaa5 = false;
        try {
            CoocaaVersion cv = CoocaaApplication.getCoocaaVersion();
            isCooCaa5 = (cv.major == 5);
            // CoocaaExtentionAPI coocaaApi = CoocaaExtentionAPI.newInstance();
            // if(coocaaApi != null)
            // {
            // isCcSystem = coocaaApi.isCoocaaSystem();
            // }
            //
            // CoocaaVersion coocaaVersion = CoocaaApplication.getCoocaaVersion();
            // if(isCcSystem && coocaaVersion.major == 5)
            // {
            // isCooCaa5 = true;
            // }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return isCooCaa5;
    }

    public boolean getAppStoreIsHomePage() {
        try {
            String result = LiteContentServiceConnecter.getService().call(
                    "setting://com.coocaa.launcher/current_launcher?action=get");
            LogUtils.d("wait", "wait getAppStoreIsHomePage:" + result);
            Current_LauncherData data = Current_LauncherData.parseJObject(result,
                    Current_LauncherData.class);
            if (data != null && data.error_code == 0 && data.resource.total_count > 0) {
                int i = 0;
                for (i = 0; i < data.resource.resource_items.size(); i++) {
                    if (data.resource.resource_items.get(i).pkgname.equals("com.tianci.appstore")) {
                        return true;
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return false;
    }

    private static class Current_LauncherData extends JObject {
        public static class Current_LauncherDataRes extends JObject {
            public static class Current_LauncherDataResItem extends JObject {
                public String activity, pkgname;
            }

            public List<Current_LauncherDataResItem> resource_items;
            public int total_count;
        }

        public int error_code;
        public String error_msg;
        public Current_LauncherDataRes resource;
    }
}
